/*
* Copyright 2016 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-wechat-open-api
*
* @File name : VehicleBindingController.java
*
* @Author : LiuJun
*
* @Date : 2016年12月5日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2016年12月5日    LiuJun    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmcc.gateway.wechat.controller.customerinfo;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.marketing.common.dto.customerInfo.CarInfoDto;
import org.marketing.common.dto.customerInfo.VehicleBindingParamDto;
import org.marketing.common.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yonyou.gmcc.gateway.wechat.common.ApiVersionConsts;
import com.yonyou.gmcc.gateway.wechat.controller.BaseController;
import com.yonyou.gmcc.gateway.wechat.service.interfaces.VehicleBindingApServiceInterface;
import com.yonyou.gmcc.gateway.wechat.util.ReflectionUtils;

/**
*
* @author LiuJun
* 车辆绑定API-controller
* @date 2016年12月5日
*/
@RestController
@RequestMapping(value = ApiVersionConsts.BASE_PATH+"/vehicleBinding" )
public class VehicleBindingController extends BaseController{
    
    @Autowired
    private VehicleBindingApServiceInterface vehicleBindingApServiceInterface;
    
    
    /**
    *
    * @author LiuJun
    * 判断潜客和车主是否绑定
    * @date 2016年12月5日
    * @param potentialUserId 潜客用户ID
    * @param phone 车主手机号码
    * @return
    * @throws Exception
    */
    @RequestMapping(value="/whetherBinding",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public @ResponseBody Map<String,Object> judgeVehicleWhetherBinding(@RequestParam("phone") String phone,
                                                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long potentialUserId = (long)87;
        return vehicleBindingApServiceInterface.judgeVehicleWhetherBinding(potentialUserId, phone);
    }
    
    
    /**
    *
    * @author LiuJun
    * 潜客和车主绑定
    * @date 2016年12月5日
    * @param reuqestBody
    * @return
    * @throws Exception
    */
    @HystrixCommand(groupKey="vehicleBinding",fallbackMethod = "vehicleBindingHandleFallback")
    @RequestMapping(method = RequestMethod.POST,consumes="application/json;charset=UTF-8",produces = "application/json;charset=UTF-8")
    public @ResponseBody Map<String,Object> vehicleBindingHandle(@RequestBody  VehicleBindingParamDto vehicleBindingParamDto,
                                                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long potentialUserId = (long)87;
        if(potentialUserId!=null && vehicleBindingParamDto.getPotentialUserId()==null){
            vehicleBindingParamDto.setPotentialUserId(potentialUserId);
        }
        vehicleBindingParamDto.setDealerCode(this.getDealerCode(request));
        vehicleBindingParamDto.setPotentialUserId(potentialUserId);
//        CommonUtils.toString(vehicleBindingParamDto);
        return vehicleBindingApServiceInterface.vehicleBindingHandle(vehicleBindingParamDto);
    }
    
    @SuppressWarnings("unused")
    private Map<String,Object> vehicleBindingHandleFallback(VehicleBindingParamDto vehicleBindingParamDto,HttpServletRequest request, HttpServletResponse response,Throwable e){
        ReflectionUtils.setFieldValue(e, "detailMessage", e.getMessage());
        throw new RuntimeException(e.getMessage(),e);
    }


    /**
    * 更新车辆信息
    * @author QianKun
    * @date 2017年1月12日
    * @param carInfoDto
    * @return
    * @throws Exception
    */
    @RequestMapping(value={"/updateCarInfo"},method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> updateCarInfo(@RequestBody CarInfoDto carInfoDto)throws Exception{
        Map<String,Object> map = vehicleBindingApServiceInterface.updateCarInfo(carInfoDto);
        return map;
    }
    
    /**
    * 解除车主车辆绑定
    * @author QianKun
    * @date 2017年1月12日
    * @param carInfoDto
    * @return
    * @throws Exception
    */
    @RequestMapping(value={"/updateOwnerCar"},method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> updateOwnerCar(@RequestBody CarInfoDto carInfoDto)throws Exception{
        Map<String,Object> map = vehicleBindingApServiceInterface.updateOwnerCar(carInfoDto);
        return map;
    }
}
